package com.vlin.controller.tuya;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.vlin.api.client.Client;
import com.vlin.api.client.Method;
import com.vlin.api.client.Params;
import com.vlin.service.tuya.TuyaService;

@RestController
public class TuyaAPIController implements TuyaService {

	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private Client client;

	@Override
	public String getUsers(int limit, int size) {
		if (logger.isDebugEnabled()) {
			logger.debug("send GET app/users request to tuya server");
		}

		return client.send("/v1.0/apps/vlinsmart/users",
				Params.build().setParam("page_no", String.valueOf(limit)).setParam("page_size", String.valueOf(size)),
				Method.GET).toString();
	}

	@Override
	public String getUserDevice(String uid) {
		if (logger.isDebugEnabled()) {
			logger.debug("send GET app/user/device request to tuya server");
		}
		return client.send(String.format("/v1.0/users/%s/devices", uid), Params.build(), Method.GET).toString();
	}

	@Override
	public String getUserToken(String deviceId) {
		
		
		if (logger.isDebugEnabled()) {
			logger.debug("send GET app/devices/token request to tuya server");
		}
		return client.send("/v1.0/devices/token", Params.build(), Method.POST).toString();
	}

	@Override
	public String getDeviceFunction(String did) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/device/function request to tuya server");
		}
		return client.send("/v1.0/devices/" + did+ "/functions", Params.build(), Method.GET).toString();
	}
	
	public static final String TOKEN_DEVICES = "/v1.0/devices/tokens/%s";

	@Override
	public String getTokenDevices(String token) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/token/devices request to tuya server");
		}
		return client.send(String.format(TOKEN_DEVICES, token), Params.build(), Method.GET).toString();
	}

	public static final String SEND_COMMONDS = "/v1.0/devices/%s/commands";
	
	@Override
	public String sendCommonds(String did, String body) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/device/commonds request to tuya server");
		}
		return client.send(String.format(SEND_COMMONDS, did), Params.build().setParam("body", body), Method.POST).toString();
	}

	private static final String REMOVE_DEVICE = "/v1.0/devices/%s";
	
	@Override
	public String deleteDevice(String did) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/device/delete request to tuya server");
		}
		return client.send(String.format(REMOVE_DEVICE, did), Params.build(), Method.GET).toString();
	}
	
	private static final String DEVICE_STATUS = "/v1.0/devices/%s/status";

	@Override
	public String deviceStatus(String did) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/device/status request to tuya server");
		}
		return client.send(String.format(DEVICE_STATUS, did), Params.build(), Method.GET).toString();
	}
	
	private static final String DEVICE_INFO = "/v1.0/devices/%s";

	@Override
	public String deviceInfo(String did) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/device/info request to tuya server");
		}
		return client.send(String.format(DEVICE_INFO, did), Params.build(), Method.GET).toString();
	}
	
	private static final String DEVICES_STATUS = "/v1.0/devices/status";

	@Override
	public String devicesStatus(List<String> dids) {
		if(logger.isDebugEnabled()) {
			logger.debug("send app/devices/status request to tuya server");
		}
		return client.send(DEVICES_STATUS, Params.build(), Method.GET).toString();
	}

}
